package com.Telien.app.ui.user;


import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.Telien.app.ui.main.MainApp;
import com.Telien.app.user.service.UserManagementService;

public class UserLogInDialog extends JDialog implements ActionListener{

	private final JPanel contentPanel = new JPanel();
	NewUserDialog nud;
	private JButton btnLogin;
	private JButton btnCancel;
	private JButton btnNewUser;
	private JLabel lblUserName;
	private JLabel lblTelienLogin;
	private JLabel lblPassword;
	private JTextField usernameField;
	private JPasswordField passwordField;
	private JLabel errorMsgLabel;
	MainApp mainApp;
	
	UserManagementService ums;

	/**
	 * Launch the application.
	 *
	public static void main(String[] args) {
		try {
			UserLogInDialog dialog = new UserLogInDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public UserLogInDialog() {
		setSize(600,500);
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setVisible(true);
		setAlwaysOnTop(true);
		setModal(true);
		getContentPane().setLayout(null);
		
		lblUserName = new JLabel("User Name:");
		lblUserName.setBounds(12, 56, 78, 25);
		getContentPane().add(lblUserName);
		
		usernameField = new JTextField();
		usernameField.setBounds(112, 56, 200, 25);
		getContentPane().add(usernameField);
		usernameField.setColumns(10);
		
		lblPassword = new JLabel("Password:");
		lblPassword.setBounds(12, 104, 78, 16);
		getContentPane().add(lblPassword);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(112, 99, 200, 27);
		getContentPane().add(passwordField);
		
		errorMsgLabel = new JLabel();
		errorMsgLabel.setBounds(50, 190, 200, 27);
		errorMsgLabel.setVisible(false);
		errorMsgLabel.setForeground(Color.RED);
		getContentPane().add(errorMsgLabel);		
		
		btnLogin = new JButton("Login");
		btnLogin.setBounds(38, 281, 97, 25);
		btnLogin.addActionListener(this);
		getContentPane().add(btnLogin);
		
		btnNewUser = new JButton("New User");
		btnNewUser.setBounds(147, 281, 97, 25);
		btnNewUser.addActionListener(this);
		getContentPane().add(btnNewUser);
		
		btnCancel = new JButton("Cancel");
		btnCancel.setBounds(262, 281, 97, 25);
		btnCancel.addActionListener(this);
		getContentPane().add(btnCancel);
		
		lblTelienLogin = new JLabel("Telien Login");
		lblTelienLogin.setBounds(152, 13, 78, 16);
		getContentPane().add(lblTelienLogin);
	}
	
	public void actionPerformed(ActionEvent e){
		if (e.getSource() == btnCancel){
			dispose();
		}
		else if(e.getSource() == btnLogin){
			UserManagementService userMgmtService = new UserManagementService();
			if (userMgmtService.login(usernameField.getText(), passwordField.getText() ) ){
				//TODO use encrypted password - passwordField.getPassword().toString()
				mainApp = new MainApp(userMgmtService.getUserObj(usernameField.getText()));
				mainApp.getFrame().setVisible(true);
				mainApp.getUserLogInDialog().dispose();
				dispose();
			}
			else{
				errorMsgLabel.setVisible(true);
				errorMsgLabel.setText("Authenication failure! Please confirm your username and password.");
				errorMsgLabel.setSize(errorMsgLabel.getPreferredSize());
			}
		}
		else if(e.getSource() == btnNewUser){
			dispose();
			JDialog newUser = new NewUserDialog();
			newUser.setVisible(true);
		}		
	}
	
	public MainApp getMainFrame() {
		return mainApp;
	} 
}
